<template>
  <div>
    <!-- 台桌表格 -->
    <div class="table">
      <el-table :data="lists" stripe style="width: 100%">
        <!-- id -->
        <el-table-column prop="_id" label="ID" width="260" />
        <!-- 序号 -->
        <el-table-column prop="index" label="序号" sortable />
        <!-- 名称 -->
        <el-table-column prop="name" label="名称" />
        <!-- 分类 -->
        <el-table-column prop="serie.name" label="分类" />
        <!-- 价格 -->
        <el-table-column prop="price" label="价格" width="150" />
        <!-- 是否上架 -->
        <el-table-column label="是否上架" width="250">
          <template slot-scope="scope">
            <!-- 函数柯里化就是在有默认参数的函数嵌套新函数，再传其他值 -->
            <el-switch
              active-color="#13ce66"
              inactive-color="#ff4949"
              v-model="scope.row.status"
              @change="
                (flag) => {
                  onCStatus(flag, scope.row._id);
                }
              "
            >
            </el-switch>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="250">
          <template slot-scope="scope">
            <el-link type="primary" @click="onRedact(scope.$index, scope.row)"
              >编辑</el-link
            >
            <span class="nbsb"></span>
            <el-link type="danger" @click="onDel(scope.$index, scope.row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑对话框 -->
    <el-dialog title="编辑菜品" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
        <!--序号  -->
        <el-form-item label="序号" :label-width="formLabelWidth" prop="index">
          <el-input v-model="ruleForm.index" autocomplete="off"></el-input>
        </el-form-item>
        <!--名称  -->
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 价格 -->
        <el-form-item label="价格" :label-width="formLabelWidth" prop="price">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
        <!-- 分类 -->
        <el-form-item label="分类" :label-width="formLabelWidth" prop="serie">
          <el-select v-model="ruleForm.serie" placeholder="请选择菜品分类">
            <el-option
              v-for="(v, i) in typeList"
              :label="v.name"
              :value="v._id"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- 是否上架 -->
        <el-form-item label="是否上架" :label-width="formLabelWidth">
          <el-switch v-model="ruleForm.status"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="ensure()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, delDish, updatedDish, getType } from "@/api/dish";
export default {
  data() {
    return {
      // 菜品分类列表
      typeList: [],
      lists: [],
      dialogFormVisible: false,
      formLabelWidth: "120px",
      name: "LLL",
      table: {
        name: "",
        index: 0,
        _listIndex: 0,
        id: "",
        user: "",
        status: "",
        price: "",
      },
      /* 菜品信息 */
      ruleForm: {
        name: "",
        index: "",
        status: false,
        serie: "",
        img: "",
        price: "",
        _listIndex: 0,
        id: "",
        user: "",
      },
      /* 检验规则 */
      rules: {
        name: [{ required: true, message: "请输入菜品名称", trigger: "blur" }],
        price: [{ required: true, message: "请输入菜品价格", trigger: "blur" }],
        index: [
          { required: true, message: "请输入菜品序号", trigger: "change" },
        ],
        imgUrl: [
          {
            required: true,
            message: "请输入菜品图片url地址",
            trigger: "change",
          },
        ],
        type: [{ required: true, message: "请选择菜品分类", trigger: "blur" }],
      },
    };
  },
  mounted() {
    //   请求列表数据
    getList()
      .then((res) => {
        this.lists = res.data;
      })
      .catch((err) => {
        this.$message.error("请求失败");
      });

    // 获取菜品分类
    getType()
      .then((res) => {
        this.typeList = res.data;
      })
      .catch((err) => {
        this.$message.error("获取菜品分类失败！！");
      });
  },
  methods: {
    //   编辑
    onRedact(index, row) {
      // 显示退出编辑框
      this.dialogFormVisible = true;
      //   与弹出框数据绑定
      this.ruleForm.name = row.name;
      this.ruleForm.index = row.index;
      this.ruleForm._listIndex = index;
      this.ruleForm.id = row._id;
      this.ruleForm.status = row.status;
      this.ruleForm.price = row.price;
      this.ruleForm.serie = row.serie._id;
    },
    // 删除
    onDel(index, row) {
      this.$confirm("此操作将永久删除该台桌, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 发送删除请求
          delDish({ id: row._id })
            .then((res) => {
              // 删除列表中的数据
              this.lists.splice(index, 1);
              //   退出删除成功
              this.$message({
                type: "success",
                message: res.msg,
              });
            })
            .catch((err) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑-取消
    cancel() {
      // 关闭
      this.dialogFormVisible = false;
    },
    //编辑-确认
    ensure() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          // 发起修改
          updatedDish({
            id: this.ruleForm.id,
            index: this.ruleForm.index,
            name: this.ruleForm.name,
            status: this.ruleForm.status,
            price: this.ruleForm.price,
            serie: this.ruleForm.serie,
          })
            .then((res) => {
              //  弹出提示msg
              this.$message({
                message: res.msg,
                type: "success",
              });
              //   改变列表的值
              let index = this.ruleForm._listIndex;
              this.lists[index].index = res.data.index;
              this.lists[index].name = res.data.name;
              this.lists[index].status = res.data.status;
              this.lists[index].price = res.data.price;
              // 因为列表中serie是菜品分类对象
              for (const v of this.typeList) {
                //根据id判断类别
                if (res.data.serie == v._id) {
                  // 相等将整个对象赋值
                  this.lists[index].serie = v;
                  break;
                }
              }

              //   关闭编辑框
              this.dialogFormVisible = false;
            })
            .catch((err) => {
              this.$message.error("编辑错误！！");
            });
        }
      });
    },
    // 是否上架
    onCStatus(flag, id) {
      updatedDish({
        id,
        status: flag,
      })
        .then((res) => {
          //  弹出提示msg
          this.$message({
            message: res.msg,
            type: "success",
          });
        })
        .catch((err) => {
          this.$message.error("编辑上架错误！！");
        });
    },
  },
};
</script>

<style scoped lang="scss">
.table {
  width: 100%;
  // 间隔
  .nbsb {
    display: inline-block;
    width: 10px;
  }
}
</style>